/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.basic.pages.system;

import com.eelpo.cmmp.basic.dao.vo.SystemConfig;
import com.eelpo.cmmp.basic.services.ISystemConfigManageService;
import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.core.wicket.component.label.FeedbackLabel;
import com.eelpo.cmmp.core.wicket.page.extensions.SimpleWebPage;
import com.eelpo.common.util.BlankUtils;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * <pre>
 * 
 * 新增和修改系统配置页面
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class AddOrEditSystemConfig extends SimpleWebPage {

	@SpringBean
	private ISystemConfigManageService systemConfigManageService;

	/**
	 * 
	 * 新增用户构造函数
	 * 
	 */
	public AddOrEditSystemConfig() {
		this("[新增]系统配置", new SystemConfig());
	}

	/**
	 * 修改用户构造函数
	 * 
	 * @param user
	 *            用户
	 */
	public AddOrEditSystemConfig(final SystemConfig config) {
		this("[修改]系统配置", config);
	}

	/**
	 * @param title
	 *            页面标题
	 * @param user
	 *            新增或修改的用户
	 */
	public AddOrEditSystemConfig(String title, final SystemConfig config) {
		super(title);
		add(renderFormContainer(config));
	}

	public Form<SystemConfig> renderFormContainer(final SystemConfig config) {

		Form<SystemConfig> form = new Form<SystemConfig>("form", new Model<SystemConfig>(config));

		renderFeedbackPanel(form);
		renderConfigKeyTextField(form);
		renderConfigValueTextField(form);
		renderDescriptionTextArea(form);
		renderHintTextArea(form);
		renderSaveButton(form);
		renderBackButton(form);

		return form;
	}

	public void renderFeedbackPanel(Form<SystemConfig> form) {
		form.add(renderFeedbackPanel("feedback"));
	}

	public void renderConfigKeyTextField(Form<SystemConfig> form) {
		TextField<String> txtConfigKey = new TextField<String>("txtConfigKey", new PropertyModel<String>(form.getDefaultModelObject(), "configKey"));
		txtConfigKey.setMarkupId("txtConfigKey");
		form.add(new FeedbackLabel("txtConfigKeyFeedback", txtConfigKey));
		form.add(txtConfigKey);
	}

	public void renderConfigValueTextField(Form<SystemConfig> form) {
		TextField<String> txtConfigValue = new TextField<String>("txtConfigValue", new PropertyModel<String>(form.getDefaultModelObject(), "configValue"));
		txtConfigValue.setMarkupId("txtConfigValue");
		form.add(new FeedbackLabel("txtConfigValueFeedback", txtConfigValue));
		form.add(txtConfigValue);
	}

	public void renderDescriptionTextArea(Form<SystemConfig> form) {
		TextArea<String> txtDescription = new TextArea<String>("txtDescription", new PropertyModel<String>(form.getDefaultModelObject(), "description"));
		txtDescription.setMarkupId("txtDescription");
		form.add(new FeedbackLabel("txtDescriptionFeedback", txtDescription));
		form.add(txtDescription);
	}

	public void renderHintTextArea(Form<SystemConfig> form) {
		form.add(new TextArea<String>("txtHint", new PropertyModel<String>(form.getDefaultModelObject(), "hint")));
	}

	public void renderSaveButton(final Form<SystemConfig> form) {
		Button btnSave = new Button("btnSave") {
			private static final long serialVersionUID = -7414969486290041096L;

			public void onSubmit() {
				try {
					SystemConfig config = (SystemConfig) form.getDefaultModelObject();
					if (BlankUtils.isBlank(config.getId())) {
						config.setId(systemConfigManageService.addSystemConfig(config));
					} else {
						systemConfigManageService.updateSystemConfig(config);
					}
					setResponsePage(new SystemConfigManage());
				} catch (ServiceException ex) {
					info(ex.getMessage());
				} catch (Exception e) {
					e.printStackTrace();
					info("保存失败！");
				}
			}
		};

		form.add(btnSave);
	}

	public void renderBackButton(Form<SystemConfig> form) {
		Link<Void> btnBack = new Link<Void>("btnBack") {
			private static final long serialVersionUID = -7414969486290041096L;

			@Override
			public void onClick() {
				setResponsePage(new SystemConfigManage());
			}
		};

		form.add(btnBack);
	}
}
